package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwaeT;
import cn.e4j.modular.crm.entity.GwaelT;
import cn.e4j.modular.crm.entity.GwawT;
import cn.e4j.modular.crm.entity.GwawlT;
import cn.e4j.modular.crm.mapper.GwawTMapper;
import cn.e4j.modular.crm.service.GwawTService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  联系人角色 服务实现类
 * </p>
 *
 * @author miaoyl
 * @since 2019-01-11
 */
@Service
public class GwawTServiceImpl extends ServiceImpl<GwawTMapper, GwawT> implements GwawTService {

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwawlist(Page<Object> page, Dto dto) {

        List<Map<String, Object>> gwawDtos = this.baseMapper.listGwawPage(page, dto);

        return gwawDtos;
    }

    @Transactional(readOnly = true)
    public GwawT getByGwaw001(String ent, String gwaw001) {
        UpdateWrapper<GwawT> gwawTUpdateWrapper = new UpdateWrapper<>();
        gwawTUpdateWrapper.eq("GWAWENT",ent);
        gwawTUpdateWrapper.eq("GWAW001",gwaw001);
        GwawT gwaw = this.baseMapper.selectOne(gwawTUpdateWrapper);
        return gwaw;
    }

    @Transactional
    @Override
    public void add(GwawT gwaw,GwawlT gwawl) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwaw.setGWAWENT(shiroUser.getDBC());
        gwaw.setGWAW002(" ");
        gwaw.setGWAW003(" ");//预留栏位2
        gwaw.setGWAWUSER(shiroUser.getACCOUNT());
        gwaw.setGWAWGRUP(shiroUser.getORG_ID());
        gwaw.setGWAWDATE(LocalDate.now());
        gwaw.setGWAWTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        gwaw.setGWAWORIG(shiroUser.getORG_ID());
        gwaw.setGWAWORIU(shiroUser.getACCOUNT());

        gwawl.setGWAWLENT(shiroUser.getDBC());
        gwawl.setGWAWL001(gwaw.getGWAW001());
        gwawl.setGWAWL002(LangUtils.getLang(shiroUser.getLANG()));
        gwaw.insert();
        gwawl.insert();
    }

    @Transactional
    @Override
    public void edit(GwawT gwaw,GwawlT gwawl) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwaw.setGWAWMODU(shiroUser.getACCOUNT());
        UpdateWrapper<GwawT> gwawTUpdateWrapper = new UpdateWrapper<>();
        gwawTUpdateWrapper.eq("GWAWENT",gwaw.getGWAWENT());
        gwawTUpdateWrapper.eq("GWAW001",gwaw.getGWAW001());

        UpdateWrapper<GwawlT> gwawlTUpdateWrapper = new UpdateWrapper<>();
        gwawlTUpdateWrapper.eq("GWAWLENT",gwaw.getGWAWENT());
        gwawlTUpdateWrapper.eq("GWAWL001",gwaw.getGWAW001());

        gwaw.update(gwawTUpdateWrapper);
        gwawl.update(gwawlTUpdateWrapper);
    }


    @Transactional
    @Override
    public void delete(GwawT gwaw,GwawlT gwawl) {
        ShiroUser shiroUser = ShiroKit.getUser();

        UpdateWrapper<GwawT> gwawTUpdateWrapper = new UpdateWrapper<>();
        gwawTUpdateWrapper.eq("GWAWENT",gwaw.getGWAWENT());
        gwawTUpdateWrapper.eq("GWAW001",gwaw.getGWAW001());

        UpdateWrapper<GwawlT> gwawlTUpdateWrapper = new UpdateWrapper<>();
        gwawlTUpdateWrapper.eq("GWAWLENT",gwaw.getGWAWENT());
        gwawlTUpdateWrapper.eq("GWAWL001",gwaw.getGWAW001());

        gwaw.delete(gwawTUpdateWrapper);
        gwawl.delete(gwawlTUpdateWrapper);
    }

    @Transactional
    @Override
    public void delDataBatch(List<GwawT> gwawTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        for(GwawT gwawT : gwawTList){
            UpdateWrapper<GwawT> gwawTUpdateWrapper = new UpdateWrapper<>();
            gwawTUpdateWrapper.eq("GWAWENT",gwawT.getGWAWENT());
            gwawTUpdateWrapper.eq("GWAW001",gwawT.getGWAW001());

            GwawlT gwawlT = new GwawlT();
            UpdateWrapper<GwawlT> gwawlTUpdateWrapper = new UpdateWrapper<>();
            gwawlTUpdateWrapper.eq("GWAWLENT",gwawT.getGWAWENT());
            gwawlTUpdateWrapper.eq("GWAWL001",gwawT.getGWAW001());

            gwawT.delete(gwawTUpdateWrapper);
            gwawlT.delete(gwawlTUpdateWrapper);
        }
    }
}
